package com.sudu.service.impl;


import com.sudu.dao.TagDao;
import com.sudu.dao.VideoDao;
import com.sudu.domain.Tag;
import com.sudu.service.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;

/**
 * 工具接口 实现类
 */

@Service
public class toolImpl implements Tool {

    @Autowired
    private TagDao tagDao ;
    @Autowired
    private VideoDao videoDao ;

    /**
     * 从文件中读取数据
     * 返回值为 字符串集合
     * @param fileName
     * @param fileIndex
     * @return
     * @throws IOException
     */
    @Override
    public String getFileData(String fileName , int fileIndex) throws IOException {
       String Data = null;
        String url = fileName +  "/" + fileIndex + ".txt";
        File file = new File(url);
        // 构造一个BufferedReader类来读取文件
        BufferedReader br = new BufferedReader(new FileReader(file));
        // str 接收每行数据
        String str = null ;
        // 使用readLine方法，一次读一行
        while ((str = br.readLine()) != null)  {
            Data += str ;
        }
        Data = Data.substring(4) ;
        return Data;
    }

    @Override
    public void uploadFile(byte[] file, String filePath, String fileName) throws IOException {

        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
            FileOutputStream out = new FileOutputStream(filePath +"/"+ fileName);
            out.write(file);
            out.flush();
            out.close();

    }


}
